//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System.ComponentModel;
using System.Configuration;
using EntLibContrib.Query.Commands;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Design;
using Container = Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.Container;

namespace EntLibContrib.Query.Configuration
{
    /// <summary>
    /// <para>Configuration data for an <c>XmlFileCommandData</c>.</para>
    /// </summary>	
    [ResourceDescription(typeof(DesignResources), "XmlFileCommandDataDescription")]
    [ResourceDisplayName(typeof(DesignResources), "XmlFileCommandDataDisplayName")]
	public class XmlFileCommandData : CommandData
	{
        private const string filenameProperty = "filename";
        private const string elementProperty = "element";

		/// <summary>
		/// Initializes a new instance of the <see cref="XmlFileCommandData"/> class.
		/// </summary>
		public XmlFileCommandData()
            : this(null)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="XmlFileCommandData"/> class.
		/// </summary>
		/// <param name="name">The provider name.</param>
		public XmlFileCommandData(string name)
			: base(name, typeof(XmlFileCommand))
		{
		}

        /// <summary>
        /// Name of the data provider.
        /// </summary>
        [ConfigurationProperty(nameProperty, DefaultValue = "Xml File Command")]
        public override string Name
        {
            get { return base.Name; }
            set { base.Name = value; }
        }

		/// <summary>
		/// Gets or sets the filename.
		/// </summary>
        [ConfigurationProperty(filenameProperty, IsRequired = true)]
        [ResourceDescription(typeof(DesignResources), "XmlFileCommandFilenameDescription")]
        [ResourceDisplayName(typeof(DesignResources), "XmlFileCommandFilenameDisplayName")]
        [Editor(CommonDesignTime.EditorTypes.FilteredFilePath, CommonDesignTime.EditorTypes.UITypeEditor)]
		public string Filename
		{
            get { return (string)base[filenameProperty]; }
            set { base[filenameProperty] = value; }
		}

        /// <summary>
        /// Gets or sets the element.
        /// </summary>
        [ConfigurationProperty(elementProperty, IsRequired = true)]
        [ResourceDescription(typeof(DesignResources), "XmlFileCommandDataElementDescription")]
        [ResourceDisplayName(typeof(DesignResources), "XmlFileCommandDataElementDisplayName")]
        public string Element
        {
            get { return (string)base[elementProperty]; }
            set { base[elementProperty] = value; }
        }

		/// <summary>
		/// A <see cref="TypeRegistration"/> container configuration model for <see cref="XmlFileCommandData"/>.
		/// </summary>
		/// <returns>A <see cref="TypeRegistration"/> for registering a <see cref="XmlFileCommandData"/> in the container.</returns>
        public override TypeRegistration GetRegistration()
		{
			return
				new TypeRegistration<ICommand>(
                    () => new XmlFileCommand(
                        Element, 
                        Filename,
                        Container.ResolvedIfNotNull<IParameterSet>(ParameterSetName)))
				{
                    Lifetime = TypeRegistrationLifetime.Transient,
                    Name = Name,
                    IsPublicName = true
				};
		}
	}
}